package com.bridgeintelligent.tag.webserver.dim.query.service;

import com.bridgeintelligent.tag.webserver.dim.query.api.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by JackCheung on 2019/10/21 5:43 PM.
 */
@Api(tags = "标签查询")
@AllArgsConstructor
@RestController
@RequestMapping(value = "/api/query")
public class QueryController {

    private QueryApiUtils queryApiUtils;

    @ApiOperation(value = "数据集查询", notes = "标签查询")
    @PostMapping("/dataSet")
    public DataSetRsp dataSet(String config) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.dataSet(config);
    }

    @ApiOperation(value = "客群分析", notes = "标签查询")
    @PostMapping("/customerGroup")
    public CustomerGroupRsp customerGroup(String config) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.customerGroup(config);
    }

    @ApiOperation(value = "微观画像", notes = "标签查询")
    @PostMapping("/microPortrait")
    public MicroPortraitRsp microPortrait(String filterName, String filterValue) {
        MicroPortraitRsp resp = new MicroPortraitRsp();
        Map<String,String> map = checkType(filterName,filterValue);
        if (map.get("flag").equals("false")){
            resp.setFlag(map.get("flag"));
            resp.setMsg(map.get("msg"));
            resp.setCode(0);
            return resp;
        }
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        MicroPortraitRsp dbResp = queryApi.microPortraitRsp(filterName, filterValue);
        if (CollectionUtils.isEmpty(dbResp.getData().getContent())){
            dbResp.setFlag("false");
            dbResp.setMsg("无此客户号查询结果，请重新输入");
        }else {
            dbResp.setFlag("true");
        }
        return dbResp;
    }

    @ApiOperation(value = "聚合分析", notes = "标签查询")
    @PostMapping("/index")
    public IndexRsp index(String config) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.index(config);
    }

    @ApiOperation(value = "客群洞察", notes = "标签查询")
    @PostMapping("/customersAnalysis")
    public DimAnalysisRsp customersAnalysis(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.customersAnalysis(condition);
    }

    @ApiOperation(value = "客群洞察", notes = "标签查询")
    @PostMapping("/customersMultiAnalysis")
    public MultiDimAnalysisRsp customersMultiAnalysis(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.customersMultiAnalysis(condition);
    }

    public Map<String,String> checkType(String cardType,String cardNum){
        Map<String,String> map = new HashMap<>();
        map.put("flag","true");
        map.put("msg","输入正确");
        return map;
    }

    @ApiOperation(value = "客户信息查询", notes = "客户信息查询")
    @PostMapping("/customerDetail")
    public CustomerDetailRsp customerDetail(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.customerDetail(condition);
    }

    @ApiOperation(value = "客群洞察-根据某个标签二次聚合", notes = "标签查询")
    @PostMapping("/customersDimAggregation")
    public DimAggregationRsp customersDimAggregation(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.customersDimAggregation(condition);
    }


    @ApiOperation(value = "画像探索分析", notes = "标签查询")
    @PostMapping("/analysisAggregation")
    public AnalysisAggregationRsp analysisAggregation(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.analysisAggregation(condition);
    }

    @ApiOperation(value = "画像探索分-筛选客群", notes = "标签查询")
    @PostMapping("/analysisCount")
    public AnalysisAggregationRsp analysisCount(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.analysisCount(condition);
    }

    @ApiOperation(value = "全局刷新缓存", notes = "全局刷新缓存")
    @PostMapping("/refresh")
    public CommonRsp refresh() {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.refresh();
    }

    @ApiOperation(value = "lookLike分组数据查询", notes = "标签查询")
    @PostMapping("/lookLikeAggeragtion")
    public LookLikeResult lookLikeAggeragtion(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.lookLikeAggeragtion(condition);
    }

    @ApiOperation(value = "lookLike查询index下的分数最大值最小值", notes = "标签查询")
    @PostMapping("/lookLikeMinMax")
    public LookLikeResult lookLikeMinMax(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.lookLikeMinMax(condition);
    }


    @ApiOperation(value = "主题明细数据查询", notes = "标签查询")
    @PostMapping("/themeDetail")
    public ThemeDetailResult themeDetail(String condition) {
        QueryApi queryApi = queryApiUtils.build(QueryApi.class);
        return queryApi.themeDetail(condition);
    }

}
